
package com.njkwt.business.system.service;

import com.njkwt.business.system.domain.Org;
import com.njkwt.business.system.domain.OrgExample;
import com.njkwt.business.system.dto.OrgDto;
import com.njkwt.business.system.dto.PageDto;
import com.njkwt.business.system.mapper.OrgMapper;
import com.njkwt.util.CopyUtil;
import com.njkwt.util.UuidUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Date;

/**
* @ClassName  OrgService
* @Description
* @Author ydj (171799383@qq.com)
* @Date 2020-8-18 12:38:41
* @Version V1.0
**/

@Service
public class OrgService {

    @Resource
    private OrgMapper orgMapper;

    /**
     * 列表查询
     */
    public void list(PageDto pageDto) {
        PageHelper.startPage(pageDto.getPage(), pageDto.getSize());
        OrgExample orgExample = new OrgExample();
        List<Org> orgList = orgMapper.selectByExample(orgExample);
        PageInfo<Org> pageInfo = new PageInfo<>(orgList);
        pageDto.setTotal(pageInfo.getTotal());
        List<OrgDto> orgDtoList = CopyUtil.copyList(orgList, OrgDto.class);
        pageDto.setList(orgDtoList);
    }

    /**
     * 保存，id有值时更新，无值时新增
     */
    public void save(OrgDto orgDto) {
        Org org = CopyUtil.copy(orgDto, Org.class);
        if (StringUtils.isEmpty(orgDto.getId())) {
            this.insert(org);
        } else {
            this.update(org);
        }
    }

    /**
     * 新增
     */
    private void insert(Org org) {
        Date now = new Date();
        org.setId(UuidUtil.getShortUuid());
        orgMapper.insert(org);
    }

    /**
     * 更新
     */
    private void update(Org org) {
        orgMapper.updateByPrimaryKey(org);
    }

    /**
     * 删除
     */
    public void delete(String id) {
        orgMapper.deleteByPrimaryKey(id);
    }
}
